<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<?
//Common::pr($this->lang->language); exit;
$language = $this->lang->language;
asort($language);
?>

<script type="text/javascript">
	Ext.onReady(function() {

		var winLanguage; // edit ablak
		var languageForm; // form panel

		var store = new Ext.data.JsonStore({
			sortInfo: {field: 'lanText', direction: 'ASC'},
			remoteSort: false,
			data: [
				<?
					$ii = 0;
					foreach($language as $lanId => $lanText) {
						echo	(($ii)?(","):(""))."{\"lanId\": \"".$lanId."\", \"lanText\": \"".$this->db->escape_str($lanText)."\"}";
						$ii++;
					}
				?>
			],
			fields: [
				{name: 'lanId'},
				{name: 'lanText'}
			]
		});

		var grid = new Ext.grid.GridPanel({
			store: store,
			id: 'grid',
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>'
			},
			columns: [
				{id: 'lanId', header: '<?=$this->db->escape_str($this->lang->line("LANGUAGEID"))?>', width: 200, sortable: false, dataIndex: 'lanId', renderer: function(val) { return val.split('_').join(' '); }},
				{id: 'lanText', header: '<?=$this->db->escape_str($this->lang->line("TEXT"))?>', width: 300, sortable: false, dataIndex: 'lanText', renderer: function(val) {
					return val.split('<').join('&lt;').split('>').join('&gt;');
				}}
			],
			stripeRows: true,
			autoExpandColumn: 'lanText',
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			listeners: {

				rowdblclick: function(grid, rowIndex, event) {
					if(rowIndex != 'new') {
						var record = grid.getStore().getAt(rowIndex);
					} else {
						var record = new Object();
						record.data = new Object();
					}

					if(winLanguage) {
						winLanguage.close();
					}
					winLanguage = new Ext.Window({
						iconCls: (rowIndex == 'new')?('application_form_add'):('application_form_edit'),
						layout:'fit',
						modal: true,
						width:600,
						floating: true,
						minWidth: 600,
						height:250,
						minHeight: 300,
						closeAction:'close',
						maximizable:true,
						title: '<?=$this->db->escape_str($this->lang->line("EDITING_TEXT"))?>',

						listeners: {
							close: function() {
								//Ext.get('center').load({url: '<?=site_url()?>admin/language', scripts:true});
								Ext.getCmp('tabs').getActiveTab().getUpdater().refresh()
							}
						},

						items:[{

									border: false,

									items: languageForm = new Ext.FormPanel({
										id: 'languageForm',
										padding: 10,
										labelAlign: 'right',
										labelWidth: 110,
										autoHeight: true,
										autoWidth: true,
										border: false,
										defaultType: 'textfield',
										waitMsgTarget: true,
										items: [{
												hidden: true,
												inputType: 'hidden',
												name: 'lan_id',
												value: record.data.lanId
											}, {
												xtype:'fieldset',
												title: '<?=$this->db->escape_str($this->lang->line("TEXT"))?>',
												autoHeight:true,
												layout: {
													type: 'anchor'
												},
												items: [{
													xtype: 'textarea',
													fieldLabel: '<?=$this->db->escape_str($this->lang->line("TEXT"))?>',
													name: 'lan_text',
													id: 'lan_text',
													height: 100,
													value: record.data.lanText,
													allowBlank: false,
													blankText: '<?=$this->db->escape_str($this->lang->line("THIS_FIELD_IS_REQUIRED"))?>',
													anchor:'100%'<? /*,
													listeners: {
														render: function(editor) {
															editor.getToolbar().insertButton(editor.getToolbar().items.length, {
																iconCls: 'upload-icon',
																handler: function(b,e) {
																	Ext.Msg.prompt('Insert Image', 'Image URL', function(btn, txt) {
																		if (btn == 'ok') {
																			editor.relayCmd('insertimage', txt);
																		}
																	});                                            
																}
															});
														}
													} */ ?>
												}]
											}
										]

									})

								}

							],

							bbar: {
								items: [{
									text: '<?=$this->db->escape_str($this->lang->line("SAVE"))?>',
									handler: save,
									icon: '<?=MEDIA?>icon/disk.png'
								}, {
									xtype: 'tbseparator'
								},{
									text: '<?=$this->db->escape_str($this->lang->line("CANCEL"))?>',
									handler: close,
									icon: '<?=MEDIA?>icon/cancel.png'
								}, {
									xtype: 'tbseparator'
								},{
									text: '<?=$this->db->escape_str($this->lang->line("RESET"))?>',
									handler: function() {
										languageForm.getForm().reset();
									}
								}],
								buttonAlign : 'right'
							}

					});
					winLanguage.show(this);

				}

			},

			border: false,
			frame:false,

			tbar: {
				items: [{
						icon: '<?=MEDIA?>icon/arrow_refresh_small.png',
						text: '<?=$this->db->escape_str($this->lang->line(($this->input->cookie("language_transposed"))?("SWITCH_BACK_TO_NORMAL_MODE"):("SWITCH_FROM_TEXT_TO_ID")))?>',
						ref: '../changeBtn',
						handler: function() {
							if(Ext.util.Cookies.get('language_transposed')) {
								Ext.util.Cookies.clear('language_transposed');
								Ext.util.Cookies.set('language_transposed', null, new Date("January 1, 1970"));
								grid.changeBtn.setText('<?=$this->db->escape_str($this->lang->line("SWITCH_FROM_TEXT_TO_ID"))?>');
							} else {
								Ext.util.Cookies.set('language_transposed', 1);
								grid.changeBtn.setText('<?=$this->db->escape_str($this->lang->line("SWITCH_BACK_TO_NORMAL_MODE"))?>');
							}
						}
					}
				]
			},

			plugins:[
				new Ext.ux.grid.Search({
					minChars:2,
					mode: 'local',
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:200,
					disableIndexes:[],
					autoFocus:true
				})]

		});

		grid.render('grid-table-language');

		function save() {
			if(!languageForm.getForm().isValid()) {
				Ext.Msg.show({
					title:'<?=$this->db->escape_str($this->lang->line("ERROR"))?>',
					msg: '<?=$this->db->escape_str($this->lang->line("PLEASE_CORRECT_THE_ERRORS"))?>',
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.WARNING
				});
			} else {
				if(languageForm.getForm().isDirty()) {
					languageForm.getForm().submit({
						url: '<?=site_url()?>admin/language_insert_update',
						success: function(form, resp){
							if(resp.result.error) {
								Ext.Msg.show({
									title:'<?=$this->db->escape_str($this->lang->line("ERROR"))?>',
									msg: resp.result.error,
									buttons: Ext.Msg.OK,
									icon: Ext.MessageBox.ERROR
								});
							} else {
								winLanguage.close();
							}
						}
					});
				} else {
					winLanguage.close();
				}
			}
		};

		function close() {
			winLanguage.close();
		};

	})
</script>

<div id="grid-table-language"></div>